/**
 * Aggregation.java
 * ************************** 
 * @date Mar 31, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.aggregation;

import gossipServices.aggregation.functions.AggregationUpdateFunction;
import gossipServices.basic.view.PartialView;
import gossipServices.gossipThread.GossipBasedProtocol;
import gossipServices.network.NetworkServicesAPI;
import gossipServices.pss.PeerSamplingServiceAPI;

/**
 * Description: 
 * This class instantiates the two gossip thread
 * of the Aggregation algorithm.
 * It uses the AggActiveThread and AggPassiveThread 
 * classes. It extends the GossipBasedProtocol,
 * so it's able to start and stop the algorithm.
 * 
 * Its implementation is close as I could do to:
 * "Gossip-Based Aggregation in Large Dynamic Networks",
 * M. Jelasity, A. Montresor, O. Babaoglu.
 * 
 *
 */
public class Aggregation extends GossipBasedProtocol
	implements AggregationService{
	
//	protected AggregationUpdateFunction updateFunction;
	
	/**
	 * @param string
	 * @param cycleLengthMsec
	 * @param aggregationView
	 * @param pss
	 * @param nia
	 * @param avgFunc
	 * @param aggStartingValue
	 */
	public Aggregation(String string, long cycleLenghtMsec,
			PartialView aggregationView, PeerSamplingServiceAPI pss,
			NetworkServicesAPI nia, AggregationUpdateFunction function,
			double aggStartingValue) {
		
		super(new AggActiveThread("AggActive", cycleLenghtMsec, aggregationView, pss, nia, 
				function,aggStartingValue),
				new AggPassiveThread("AggPassive", cycleLenghtMsec, aggregationView, pss, nia, 
						function, aggStartingValue));
//		this.updateFunction = function;
	}

	@Override
	public Class<?> getServiceClass() {
		return AggregationService.class;
	}

	@Override
	public AggregationUpdateFunction getAggregationFunction() {		
		// look we also may use the passive thread. But I cannot figure out
		// the differences on this action
		return ((AggregationService)activeThread).getAggregationFunction();
	}

	@Override
	public SingleValueDouble getLastStableValue() {
		return ((AggregationService)activeThread).getLastStableValue();
	}

	@Override
	public SingleValueDouble getActualValue() {
		return ((AggregationService)activeThread).getActualValue();
	}

}
